package com.ali8ba.common;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class AliUtil {

	/**
	 * 生成主键 32位
	 * 
	 * @return
	 */
	public final static String getUUID() {
		// jdk 1.5自带方法
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replaceAll("-", "");
		return uuid;
	}

	/**
	 * 生成验证字符串
	 * 
	 * @return
	 */
	public final static String getVolidateCode() {

		String uuid = UUID.randomUUID().toString();

		return uuid;
	}

	/**
	 * 根据秒数转化为相应的时分秒
	 * 
	 * @param second
	 * @return
	 */
	public static String timeWarp(long second) {

		long hour;

		long minute;

		StringBuilder sb = new StringBuilder("");

		hour = (long) second / 3600;

		if (hour >= 1) {
			sb.append(hour).append("小时");
			second = second - (hour * 3600);

		}

		minute = (int) second / 60;
		if (minute >= 1) {
			sb.append(minute).append("分钟");
			second = second - (minute * 60);

		}
		if (second > 0) {
			sb.append(second).append("秒");
		}

		return sb.toString();
	}

	/**
	 * 获得几天后的时间
	 * 
	 * @param days
	 *            >0的整数
	 * @return
	 */
	public final static Date getDaysAfter(int days) {

		Calendar c = Calendar.getInstance();

		c.add(Calendar.DATE, days);

		Date date = c.getTime();

		return date;
	}

	public static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 和当前时间比较,如果在当前时间之前,true
	 * 
	 * @param date
	 * @return
	 */
	public final static boolean isBeforeNow(Date date) {

		if (date == null) {

			return true;

		}
		Date d = new Date();

		if (d.after(date)) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 *            时间
	 * @param format
	 *            时间格式
	 * @return
	 */
	public final static String dateFormat(Date date, String format) {

		if (date == null) {
			return "无效时间";
		}

		if (format == null || "".equals(format.trim())) {

			format = "yyyy-MM-dd HH:mm:ss";
		}

		DateFormat dateFormat = new SimpleDateFormat(format);

		return dateFormat.format(date);
	}

	/**
	 * 得么当前日期
	 * 
	 * @return
	 */
	public final static String getCurrentDate(String suffix) {

		Date date = Calendar.getInstance().getTime();

		return dateFormat(date, "yyyyMMdd").concat(suffix);
	}

	/**
	 * 判断是是否是email
	 * 
	 * @param email
	 * @return
	 */
	public final static boolean isEmail(String email) {

		String check = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";

		Pattern regex = Pattern.compile(check);

		Matcher matcher = regex.matcher(email);

		boolean isMatched = matcher.matches();

		if (isMatched) {

			return Boolean.TRUE;

		} else {

			return Boolean.FALSE;
		}

	}

	public final static boolean isUserName(String userName) {

		// String check = "^\\w+$";
		String check = "^[\\w\\u4E00-\\u9FA5]+$";

		Pattern regex = Pattern.compile(check);

		Matcher matcher = regex.matcher(userName);

		boolean isMatched = matcher.matches();

		if (isMatched) {

			return Boolean.TRUE;

		} else {

			return Boolean.FALSE;
		}

	}

	/**
	 * 得到昨天的日期
	 * 
	 * @return
	 */
	public final static String getYesterday() {

		Calendar c = Calendar.getInstance();

		c.add(Calendar.DATE, -1);

		Date date = c.getTime();

		return dateFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 时间1 减去 时间2后 得到的秒数
	 * 
	 * @param t1
	 *            时间1
	 * @param t2
	 *            时间2
	 * @return
	 */
	public static final int getSecondsOnTimeStampReduceTimeStamp(Date t1,
			Date t2) {
		long time = t1.getTime() - t2.getTime();
		return (int) time / 1000;
	}

	/**
	 * 参数时间 减去 当前时间后 得到的秒数
	 * 
	 * @param t
	 *            减号前的时间
	 * @return
	 */
	public static final int getSecondsOnTimeStampReduceCurrentTime(Date t) {
		Date current_time = Calendar.getInstance().getTime();
		return getSecondsOnTimeStampReduceTimeStamp(t, current_time);
	}

	/**
	 * 当前时间 减去 参数时间后 得到的秒数
	 * 
	 * @param t
	 *            减号后的时间
	 * @return
	 */
	public static final int getSecondsOnCurrentTimeReduceTimeStamp(Date t) {
		Date current_time = Calendar.getInstance().getTime();
		return getSecondsOnTimeStampReduceTimeStamp(current_time, t);
	}

	/**
	 * 得到 某个日期一些秒数之后的 那个日期
	 * 
	 * @param t
	 *            某个日期
	 * @param seconds
	 *            秒数
	 * @return
	 */
	public static final Date getDateAfterDateSomeSeconds(Date t, int seconds) {
		long millSeconds = t.getTime() + seconds * 1000;
		return new Date(millSeconds);
	}
}
